#include "stdafx.h"

s_functions _GirderAPI;
p_command _GirderCommand;

static const char* PluginVersion = "1.0";


SetWinTextPlugin _AtlModule;


extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	hInstance;
    return _AtlModule.DllMain(dwReason, lpReserved); 
}

extern "C" void WINAPI gir_version(char* data, int size)
{
	strncpy_s(data, size, PluginVersion, strlen(PluginVersion));
}

extern "C" void WINAPI gir_name(char* data, int size)
{
	ATL::CStringA name;
	name.LoadString(IDS_PROJNAME);

	strncpy_s(data, size, name, name.GetLength());
}

extern "C" void WINAPI gir_description(char* data, int size)
{
	ATL::CStringA description;
	description.LoadString(IDS_DESCRIPTION);

	strncpy_s(data, size, description, description.GetLength());
}

extern "C" int WINAPI gir_devicenum()
{
	return SetWinTextPlugin::PLUGIN_ID;
}

extern "C" int WINAPI gir_requested_api(int max_api)
{
	return 1;
}

extern "C" int WINAPI gir_open(int gir_major_ver, int gir_minor_ver, int gir_micro_ver, p_functions api_functions)
{
   if(api_functions->size != sizeof(s_functions))
      return GIR_FALSE;
		
   memcpy(&_GirderAPI, api_functions, sizeof(s_functions));
   return GIR_TRUE;
}

extern "C" int WINAPI gir_close()
{
	return GIR_TRUE;
}

extern "C" void WINAPI gir_command_changed(p_command command)
{
	_GirderCommand = command;
}

extern "C" void WINAPI gir_command_gui()
{
	if(_GirderCommand == NULL)
		return;

	CommandSettings settings;
	settings.Load(_GirderCommand);

	CommandSettingsDialog settingsDialog;
	settingsDialog.SetData(settings);

	if(settingsDialog.DoModal() == IDOK)
	{
		settingsDialog.GetData(settings);
		settings.Save(_GirderCommand);
	}
}

extern "C" int WINAPI gir_info(int message, int wparam, int lparam)
{
	if(message == GIRINFO_MAINWIN_CLOSE)
	{
	}
	return GIR_TRUE;
}

extern "C" void WINAPI TargetCallback(HWND hw, p_command command)
{
	if(command != NULL)
	{
		CommandSettings settings;
		if(settings.Load(_GirderCommand))
		{
			bool parsed = false;
			ATL::CString text = settings.Text;
			if(settings.ParseText)
			{
				ATL::CStringA var(text);
				int strLength = _GirderAPI.parse_reg_string(const_cast<PCHAR>(var.GetString()), NULL, 0);
				if(strLength >= 0)
				{
					ATL::CStringA result;
					char* buff = result.GetBuffer(strLength + 1);
					if(buff != NULL)
					{
						_GirderAPI.parse_reg_string(const_cast<PCHAR>(var.GetString()), buff, strLength);
						text = result;
						parsed = true;
					}
				}
			}

			if(settings.UseSendMessage)
				::SendMessage(hw, WM_SETTEXT, 0, (LPARAM)text.GetString());
			else
				::SetWindowText(hw, text.GetString());
		}
	}
}

extern "C" int WINAPI gir_event(p_command command,
	char* eventstring, void* payload, int len, char* status, int statuslen)
{
	_GirderCommand = command;
	_GirderAPI.target_enum(SetWinTextPlugin::PLUGIN_ID, TargetCallback);
	return retContinue;
}




